package controllers;

import java.awt.event.ActionEvent;
import java.util.Vector;

import dialogs.DeleteTupleDialog;
import dialogs.InsertTupleDialog;

import models.StoreModel;

import system.View;

public class StoreController extends AbstractMenuItemController {
	private StoreModel store = new StoreModel();

	public StoreController(View v) {
		super(v);
		abstractModel = store;
		store.addExceptionListener(this);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		String actionCommand = e.getActionCommand();

		if (actionCommand.equals("Delete Store"))
			showDialogWindow(new DeleteTupleDialog(view, actionCommand, "Key",
					new String[] { "Store Name: " }, this));
		else if (actionCommand.equals("Insert Store"))
			showDialogWindow(new InsertTupleDialog(view, actionCommand,
					"Store", new String[] { "Name: ", "Address: ", "Type: " },
					this));
	}

	@Override
	public int validateInsert(Vector<String> s) {
		if (s.size() != 3)
			return VALIDATIONERROR;

		try {
			String sname;
			String address;
			String type;

			if (s.get(0).trim().length() != 0)
				sname = s.get(0).trim();
			else return VALIDATIONERROR;
			if (s.get(1).length() != 0)
				address = s.get(1).trim();
			else return VALIDATIONERROR;
			if (s.get(2).length() != 0)
				type = s.get(2).trim();
			else return VALIDATIONERROR;

			view.updateStatusBar("Inserting Store...");

			if (store.insert(sname, address, type)) return operationSuccess();
			else return operationFailure();
		} catch (NumberFormatException ex) {
			return VALIDATIONERROR;
		}
	}

	@Override
	public int validateDelete(Vector<String> s) {
		// TODO Auto-generated method stub
		if (s.size() != 1)
			return VALIDATIONERROR;

		try {
			String sname;

			if (s.get(0).trim().length() != 0)
				sname = s.get(0).trim();
			else return VALIDATIONERROR;
			view.updateStatusBar("Deleting Store...");

			if (store.delete(sname)) return operationSuccess();
			else return operationFailure();
		} catch (NumberFormatException ex) {
			return VALIDATIONERROR;
		}
	}
}